import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,ScrollView  } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../coupon/coupon.scss'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
     isload:1,
     navindex:1,
     coupontype:'RECEIVED',
     result:[],
     znwns:'',
     pageNumber:0,
     hasNextPage:false,
     scrollPd:false,
     pageheight:0,
     byfrom:0,
     myresult:[]
    }
  }
  config = {
    navigationBarTitleText:'领券中心'
  }
  open(url,type){
    CommonJs.openTo(url)
  }
  componentWillMount () {
    var _this = this
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
        if(_this.$router.preload&&_this.$router.preload.byfrom=='order'){
        	_this.setState({
            byfrom:1
          })
        	Taro.setNavigationBarTitle({title:'选择可用的优惠券'})
        	_this.getmycoupon(_this.$router.preload.payAmount,_this.$router.preload.shopId,_this.$router.preload.number)
        }
        else{
        	_this.getdate(0)
        }

      }
    })
  }
  componentDidShow () {

  }
  getmycoupon(totalAmount,shopId,number){
  	var url="/order/papi/v1.9/order-coupon/prepareOrder";
  	var dates={
  		totalAmount:totalAmount,
  		shopId:shopId,
      number:number
  	}
  	var _this=this
  	CommonJs.ajaxRequest(url,dates,'PUT',function(res){
  		_this.setState({
  			myresult:res.data
  		})
  	},'wfw')
  }
  getdate(pageNumber){
    var shopid=Taro.getStorageSync('shopId');
  	var url="/crm/papi/v1.9/coupon/publish?shopId.equals="+shopid;
  	var dates={
  		size:20,
  		page:pageNumber
  	}
  	var _this=this
  	CommonJs.ajaxRequest(url,dates,'GET',function(res){
  			if (res) {

  			if(res.data.totalPages>pageNumber){
  		  	var hasNextPage = true;
  		  }
  			else{
  				var hasNextPage = false;
  			}
	      var result = res.data.list
	      var results = _this.state.result
	      for(var i = 0; i < result.length; i++){
	        results.push(result[i]);
	      }
	      if (hasNextPage) {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          scrollPd: true
	        })
	      }
	      else {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          scrollPd: false
	        })
	      }
	    } else {
	      _this.setState({
	        znwns: '暂无数据'
	      })
	    }
  	},"wfw")
  }
  loadMore(){
    var _this = this
    var pd = _this.state.scrollPd
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1
      _this.getdate(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage
      if(hasNextPage){
        Taro.showToast({
          title:'正在加载数据...',
          icon: 'none',
          duration: 2000
        })
      }else{
        Taro.showToast({
          title:'没有更多数据啦！',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
  openxq(mid,id,couponStatus){
  	this.$preload({
  		mid:mid,
  		couponid:id
  	})
  	this.open("/pages/coupon/details");
  }
  choosethis(index){
  	var myresult=this.state.myresult;
  	if(myresult[index].useable){
    	var pages=getCurrentPages();
    	var prevPage=pages[pages.length-2];
    	var obj={
    	  ids:myresult[index].verificationCode,
    	  name:myresult[index].couponName
    	}
    	prevPage.setData({
    		yhq:obj
    	})
  		Taro.navigateBack({
  			delta: 1
  		});
  	}
  	else{
  		Taro.showToast({
        title:'优惠券不可用',
        icon: 'none',
        duration: 2000
      })
  	}
  }
  render () {
    const result = this.state.result
    const myresult=this.state.myresult
    return (
      <View className='page' style={this.state.isload==1?'display:block':'display:none'}>
      	<View className='yhq-page' style={this.state.byfrom!=1?'':'display:none'}>

	        <View className='yhq-list' style={result.length>0?'':'display:none;'}>
	        	<ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
		        	{result.map((item, index) => (
		        	<View className='yhq-dl'>
	        				<View className="yhq-dt" onClick={this.openxq.bind(this,'aa',item.id,'AVAILABLE')}>

	        						<View style={item.couponType.indexOf('DISCOUNT')>=0?'':'display:none'}>
	        						<Text className="jg">{item.ruleFavourData/10}</Text>
	        						<Text className="tb"> 折</Text>
	        						</View>
	        						<View style={item.couponType.indexOf('DISCOUNT')>=0?'display:none':''}>
	        						<Text className="tb">¥</Text>
	        						<Text className="jg">{item.ruleFavourData/100}</Text>
	        						</View>
	        						<Text className="tj">{item.summary}</Text>

	        				</View>
	        				<View className="yhq-dd">
	        					<View className="yhq-time" onClick={this.openxq.bind(this,'aa',item.id,'AVAILABLE')}>
	        						<Text className="mc">{item.name}</Text>
	        						<Text className="sj">{CommonJs.FormatDate(item.beginAt,2)}到{CommonJs.FormatDate(item.expireAt,2)}</Text>
	        					</View>
	        					<View className="yhq-sy01" onClick={this.openxq.bind(this,'aa',item.id,'AVAILABLE')}>查看详情</View>
	        				</View>
	        		</View>
	        		))}
	        		</ScrollView>
	        </View>
	        <View className='yhq-list' style={result.length>0?'display:none;':''}>
	        		<View className='nodate'>暂无数据</View>
	        </View>
	      </View>

        <View className='yhq-page' style={this.state.byfrom==1?'':'display:none'}>
          <View className='yhq-list' style={myresult.length>0?'':'display:none;'}>
          	{myresult.map((item, index) => (
            	<View className={item.useable?'yhq-dl':'yhq-dl yhq-cur'}  onClick={this.choosethis.bind(this,index)}>
          				<View className="yhq-dt">
          						<View style={item.couponType.indexOf('DISCOUNT')>=0?'':'display:none'}>
          						<Text className="jg">{item.ruleFavourData/10}</Text>
          						<Text className="tb"> 折</Text>
          						</View>
          						<View style={item.couponType.indexOf('DISCOUNT')>=0?'display:none':''}>
          						<Text className="tb">¥</Text>
          						<Text className="jg">{item.ruleFavourData/100}</Text>
          						</View>
          						<Text className="tj" style={item.useable?'':'display:none'}>{item.best?'推荐使用':'可使用'}</Text>
          						<Text className="tj" style={item.useable?'display:none':''}>不可用</Text>
          				</View>
          				<View className="yhq-dd">
          					<View className="yhq-time">
          						<Text className="mc">{item.couponName}</Text>
          						<Text className="sj">{CommonJs.FormatDate(item.beginAt,2)}到{CommonJs.FormatDate(item.expireAt,2)}</Text>
          					</View>
          					<View className="yhq-sy01">点击选择</View>
          				</View>
          		</View>
          		))}

          </View>
          <View className='yhq-list' style={myresult.length>0?'display:none;':''}>
          		<View className='nodate'>暂无可用的优惠券</View>
          </View>
        </View>



      </View>
    )
  }
}
